/**
 * Created by shandianhaizan on 16/6/18.
 */

define([], function () {

    function _controller($scope, $routeParams, $location, $timeout, httpService) {


        var model ="Classification";
        $scope.data = [];
        $scope.editClassification = {};

        $scope.editObject = function(){
            $scope.editClassification.pid = "0";
        };



        $scope.update = function (scope) {
            var nodeData = scope.$modelValue;
            $scope.editClassification = nodeData;
        };


        $scope.toggle = function (scope) {
            scope.toggle();
        };

        $scope.moveLastToTheBeginning = function () {
            var a = $scope.data.pop();
            $scope.data.splice(0, 0, a);
        };


        $scope.delete = function(scope){
            var nodeData = scope.$modelValue;
            httpService.del(model, nodeData.objectId).then(function (data) {
                httpService.removeParent(model, nodeData.objectId).then(function (data) {
                    $scope.msg.show(data);
                    if($scope.isHandleSuccess(data)){
                        $scope.getAll();
                    }
                });
            });
        };

        $scope.newSubItem = function (scope) {
            var nodeData = scope.$modelValue;
            $scope.editClassification.pid = nodeData.objectId;
        };

        $scope.collapseAll = function () {
            $scope.$broadcast('angular-ui-tree:collapse-all');
        };

        $scope.expandAll = function () {
            $scope.$broadcast('angular-ui-tree:expand-all');
        };


        $scope.save = function () {
            $scope.$broadcast('show-errors-check-validity');
            if (!$scope.userForm.$valid) return;
            if($scope.editClassification) delete $scope.editClassification.nodes;
            httpService.save(model, $scope.editClassification).then(function (data) {
                $scope.msg.show(data);
                $scope.getAll();
            });
        };


        $scope.getAll = function () {
            httpService.get(model, {all:true, sort:"createdAt"}).then(function (data) {
                $scope.formatData(data.data.data);
            });

        };

        $scope.formatData = function (datas){
            var tmpObject = {};
            var tmpData = [];
            angular.forEach(datas, function(data){
                tmpObject[data.objectId] = data;
                if (data.pid && data.pid!=="0"){
                    var parent = tmpObject[data.pid];
                    if(parent){
                        parent.nodes = parent.nodes || [];
                        parent.nodes.push(data);
                    }
                }else{
                    tmpData.push(data);
                }
            });
            $scope.data = tmpData;
        };


        $scope.getAll();

        $scope.ctp = {}; //the object which list count of every  classification
        $scope.initProduction = function(){
            httpService.get("Production", {all:true}).then(function (data) {
               if($scope.isHandleSuccess(data)){
                   angular.forEach(data.data.data, function(p){
                       if(p.classification){
                           angular.forEach(p.classification, function(c){
                               $scope.ctp[c] = $scope.ctp[c] || 0;
                               $scope.ctp[c]++;
                           })
                       }
                   });
               }
            });
        };


        $scope.initProduction();





    }

    return _controller;
});

